import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router';
import { staticRoutes } from './staticRoutes';
import defaultRoutes from './defaultRoutes';
import NProgress from 'nprogress';
import 'nprogress/nprogress.css'
import store from '@/store';
//引入在axios暴露出的clearPending函数
import { clearPending } from "@/axios"

const routes: any = defaultRoutes;

console.log('routes: ', routes);
const router = createRouter({
  history: createWebHashHistory(),
  routes
});

router.beforeEach((to, from, next) => {
  clearPending();
  NProgress.start();
  let userInfo = store.getters.getUserInfo;
  if (to.path === "/login" && !!userInfo.userName == false) {
    next();
  } else {
    if (userInfo && userInfo.userName) {
      if (userInfo && router.getRoutes().length < 4) {
        staticRoutes.forEach(item => {
          router.addRoute(item);
        })
        next("/");
      } else {
        next();
      }

    } else {
      next({
        path: '/login'
      });
    }
  }
});

router.afterEach(() => {
 NProgress.done()
})
export default router;
